package com.springboot.infrastructure.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.springboot.infrastructure.mapper.TestMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TestServiceImpl implements TestService {

    private Logger logger = LoggerFactory.getLogger(TestServiceImpl.class);

    @Autowired
    private TestMapper testMapper;

    @Override
    public boolean add(Map<String, Object> mapEntity) {
        mapEntity.put("updateTime", new Date());
//        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();   //设置保存点
        int count = testMapper.add(mapEntity);
//        TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint); //回滚到保存点
//        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();  //回滚所有操作
        if (count == 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean update(Map<String, Object> mapEntity) {
        Object idObj = mapEntity.get("id");
        if (idObj == null) {
            //id不能为空
            return false;
        }
        Integer id;
        try {
            id = Integer.parseInt(idObj.toString());
        } catch (NumberFormatException e) {
            e.printStackTrace();
            //数字转换异常
            return false;
        }
        Map<String, Object> mapEntityResult = testMapper.getById(id);
        if (mapEntityResult == null || mapEntityResult.size() == 0) {
            //找不到该数据
            return false;
        }

        //循环将传递的map实体数据put进map结果实体
        //这样前端没传递的字段，就不会被设为空
        for (String key : mapEntity.keySet()) {
            mapEntityResult.put(key, mapEntity.get(key));
        }

        mapEntityResult.put("updateTime", new Date());
        int count = testMapper.update(mapEntityResult);
        if (count == 0) {  //没有更新
            return false;
        }
        return true;
    }

    @Override
    public Map<String, Object> detail(Integer id) {
        //日志级别从低到高分为：TRACE < DEBUG < INFO < WARN < ERROR < FATAL，如果设置为WARN，则低于WARN的信息都不会输出。
//        logger.trace("日志输出 trace");
//        logger.debug("日志输出 debug");
//        logger.info("日志输出 info");
//        logger.warn("日志输出 warn");
//        logger.error("日志输出 error");
        return testMapper.getById(id);
    }

    @Override
    public Map<String, Object> listPage(Integer pageNum, Integer pageSize, Map<String, Object> parameterMap) {
        PageHelper.startPage(pageNum, pageSize, true);

        Page<Map<String, Object>> resultPage = (Page<Map<String, Object>>) testMapper.query(parameterMap);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("pageNum", resultPage.getPageNum());
        resultMap.put("pageSize", resultPage.getPageSize());
        resultMap.put("pages", resultPage.getPages());
        resultMap.put("total", resultPage.getTotal());
        resultMap.put("list", resultPage);
        return resultMap;
    }


}
